﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autocentro.Entities;
using System.Data.Objects;

namespace Autocentro.Respository
{
    public class VehiculoRepository : MasterRepository, IVehiculoRepository
    {
        public List<VEHICULO> GetAllFromVehiculos()
        {
            var query = from p in context.Vehiculo
                        .Include("Cliente")
                        .Include("Marca")
                        select p;
            
            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<VEHICULO> GetFromVehiculosByCriterio(string criterio)
        {

            var query = from p in context.Vehiculo
                        .Include("Cliente")
                        .Include("Marca")
                        select p;
            if (criterio!="" && criterio!=null && criterio!=string.Empty)
            {
                query = from p in query
                        where (p.Placa.ToUpper().Contains(criterio.ToUpper())||
                        p.Cliente.RazonSocial_Nombre.ToUpper().Contains(criterio.ToUpper()))
                        select p;
            }

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();

        }

        public VEHICULO GetFromVehiculoById(int id)
        {
            return context.Vehiculo.Find(id);
        }

        public void AddVehiculo(VEHICULO vehiculo)
        {
            context.Entry(vehiculo.Cliente).State = System.Data.EntityState.Unchanged;
            context.Vehiculo.Add(vehiculo);
            context.SaveChanges();
        }

        public void UpdateVehiculo(VEHICULO vehiculo)
        {
            context.Vehiculo.Attach(vehiculo);
            context.Entry(vehiculo.Cliente).State = System.Data.EntityState.Unchanged;
            context.Entry(vehiculo.Marca).State = System.Data.EntityState.Unchanged;
            context.Entry(vehiculo).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveVehiculo(int id)
        {
            var existe = context.Vehiculo.Find(id);
            if (existe!=null)
            {
                context.Vehiculo.Remove(existe);
                context.SaveChanges();
            }

        }

        public bool ExisteDuplicidadVehiculo(string nPlaca, int id)
        {
            var query = from c in context.Vehiculo
                        where (c.Placa.ToUpper().CompareTo(nPlaca.ToUpper()) == 0 ||
                        c.Placa.ToUpper().CompareTo(nPlaca.ToUpper()) == 0) &&
                        c.Id !=id
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }


        public VEHICULO GetFromVehiculoByNPlaca(string placa)
        {
            var msg = "";
            try
            {
                var query = from v in context.Vehiculo
                            .Include("MARCA")
                            .Include("CLIENTE")
                            where v.Placa.Equals(placa)
                            select v;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                //return query.SingleOrDefault();
                return query.FirstOrDefault();
            }
            catch (Exception)
            {
                
                throw new Exception(msg);
            }

        }
    }
}
